package com.mapaoer.user.repository;

import com.mapaoer.user.entity.User;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.Optional;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

@Repository
public interface UserRepository extends BaseMapper<User> {
    
    /**
     * 根据用户名查询用户
     */
    Optional<User> findByUsername(String username);
    
    /**
     * 根据邮箱查询用户
     */
    Optional<User> findByEmail(String email);
    
    /**
     * 根据手机号查询用户
     */
    Optional<User> findByPhone(String phone);
    
    /**
     * 判断用户名是否存在
     */
    boolean existsByUsername(String username);
    
    /**
     * 判断邮箱是否存在
     */
    boolean existsByEmail(String email);
    
    /**
     * 查询用户及其关联的角色
     */
    @Select("SELECT u FROM User u LEFT JOIN FETCH u.roles WHERE u.username = #{username}")
    Optional<User> findByUsernameWithRoles(@Param("username") String username);
}